import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
const FormItem = Form.Item;
import TextInput from 'Shared/components/TextInput';
import {FORM_OPTIONS} from '../constants';
import {injectIntl, language} from './intl';
import WrappedSelect from '../common/WrappedSelect';
import {baseDataStatus} from '../Enum';
const statusList = baseDataStatus.toList(language);
class QueryPanel extends React.PureComponent {
    handleFilterChange = (value, name) => {
        this.props.onChange(name, value);
    }
    handleFilterPressEnter = (value, name) => {
        this.props.onChange(name, value);
        this.props.querySearch();
    }
    render() {
        const {formatMessage} = this.props.intl;
        return (
            <div>
                <Form className="form-standard">
                    <Row>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem label={formatMessage({
                                id: 'queryPanel.code',
                                defaultMessage: '编号'
                            })}{...FORM_OPTIONS.item}>
                                <TextInput
                                    name="code"
                                    value={this.props.condition.code}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem label={formatMessage({
                                id: 'queryPanel.name',
                                defaultMessage: '名称'
                            })} {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="name"
                                    value={this.props.condition.name}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem label={formatMessage({
                                id: 'queryPanel.form.status',
                                defaultMessage: '状态'
                            })} {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    allowClear
                                    name="status"
                                    value={this.props.condition.status}
                                    options={statusList}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={6}>
                            <Button key="query" type="primary"
                                onClick={this.props.querySearch} loading={this.props.loading}>
                                {this.props.intl.formatMessage({
                                    id: 'queryPanel.search',
                                    defaultMessage: '查询'
                                })}</Button>
                            <Button key="clear" onClick={this.props.onClickReset}>{this.props.intl.formatMessage({
                                id: 'queryPanel.reset',
                                defaultMessage: '重置'
                            })}</Button>
                        </Col>
                    </Row>
                </Form>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    condition: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    querySearch: PropTypes.func.isRequired,
    onChange: PropTypes.func.isRequired,
    onClickReset: PropTypes.func.isRequired,
    intl: PropTypes.object,
};

import {connect} from 'react-redux';
import {
    saveQueryCondition,
    querySearch,
    resetQueryPanel,
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const mapStateToProps = state => ({
    condition: getConditions(state),
    loading: state.getIn((['page', 'domainData', 'list', 'isFetching'])),
});

const mapDispatchToProps = dispatch => ({
    onChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    querySearch: () => dispatch(querySearch()),
    onClickReset: () => dispatch(resetQueryPanel()),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
